﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Text;
using System.Data;
public partial class Xuatdulieu : System.Web.UI.Page
{
    public void ExportToExcel(string fileName,string tenbang)
    {

        
        string conn = DatabaseHandler.strConnect;
        MySqlConnection con = new MySqlConnection(conn);
        MySqlDataAdapter ad = new MySqlDataAdapter("select * from "+ tenbang +"", con);
        DataSet ds = new DataSet();
        ad.Fill(ds, "tbl");





        Response.ContentType = "application/csv";
        Response.Charset = "";
        Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
        Response.ContentEncoding = Encoding.Unicode;
        Response.BinaryWrite(Encoding.Unicode.GetPreamble());
        DataTable dtb = ds.Tables["tbl"];
        try
        {
            StringBuilder sb = new StringBuilder();
            //Tạo dòng tiêu để cho bảng tính
            for (int count = 0; count < dtb.Columns.Count; count++)
            {
                if (dtb.Columns[count].ColumnName != null)
                    sb.Append(dtb.Columns[count].ColumnName);
                if (count < dtb.Columns.Count - 1)
                {
                    sb.Append("\t");
                }
            }
            Response.Write(sb.ToString() + "\n");
            Response.Flush();
            //Duyệt từng bản ghi 
            int soDem = 0;
            while (dtb.Rows.Count >= soDem + 1)
            {
                sb = new StringBuilder();

                for (int col = 0; col < dtb.Columns.Count - 1; col++)
                {
                    if (dtb.Rows[soDem][col] != null)
                        sb.Append(dtb.Rows[soDem][col].ToString().Replace(",", " "));
                    sb.Append("\t");
                }
                if (dtb.Rows[soDem][dtb.Columns.Count - 1] != null)
                    sb.Append(dtb.Rows[soDem][dtb.Columns.Count - 1].ToString().Replace(",", " "));

                Response.Write(sb.ToString() + "\n");
                Response.Flush();
                soDem = soDem + 1;
            }

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        dtb.Dispose();
        Response.End();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!Variables.loginFlag)
        //{
        //    Response.Redirect("Default.aspx");
        //    return;
        //}
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        ExportToExcel("DoanhNghiep.xls", "tbl_doanhnghiep");
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        ExportToExcel("BangO.xls", "tbl_o");
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        ExportToExcel("BangH.xls", "tbl_h");
    }
    protected void LinkButton8_Click(object sender, EventArgs e)
    {
        ExportToExcel("User.xls", "tbl_users");
    }
    protected void LinkButton9_Click(object sender, EventArgs e)
    {
        Response.Redirect("Index.aspx");
    }
    protected void LinkButton4_Click(object sender, EventArgs e)
    {
        ExportToExcel("BangT.xls", "tbl_t");
    }
    protected void LinkButton5_Click(object sender, EventArgs e)
    {
        ExportToExcel("BangM.xls", "tbl_m");
    }
    protected void LinkButton6_Click(object sender, EventArgs e)
    {
        ExportToExcel("BangF.xls", "tbl_f");
    }
    protected void LinkButton10_Click(object sender, EventArgs e)
    {
        //ExportToExcel("DulieuDN.xls", "tbl_o,tbl_h,tbl_m,tbl_t,tbl_f");
    }
}